package com.xiaoma.myaudience.biz.task;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.xiaoma.myaudience.biz.util.Constant;
import com.xiaoma.myaudience.util.HttpUtils;
import com.xiaoma.myaudience.util.Logger;
import com.xiaoma.myaudience.util.model.ModelUtils;
import com.xiaoma.myaudience.util.task.BaseDataAsyncTask;

public class UpdateUserInfoTask extends
		BaseDataAsyncTask<Map<String,String>, Void, String> {
	private final static String TAG = "UpdateUserInfoTask";
	public UpdateUserInfoTask(
			Context context,
			int what,
			DataAsyncCallback callback) {
		super(context, what, callback);
		// TODO Auto-generated constructor stub
	}
	@Override
	protected String doInBackground(Map<String,String>... params) {
		if(null==params){
			return null;
		}
		Map<String,String> ueditinfo = params[0];
		String result = null;
		Map<String,Object> returnjson = null;
        try {            
        	List<NameValuePair> values = new ArrayList<NameValuePair>();
        	String type = ueditinfo.get("type");
        	values.add(new BasicNameValuePair("type", type));
        	values.add(new BasicNameValuePair("content", ueditinfo.get("content")));
        	values.add(new BasicNameValuePair("op", "edit"));
        	if(type.equals("10")){
        		values.add(new BasicNameValuePair("authcode",ueditinfo.get("authcode")));
        		values.add(new BasicNameValuePair("status","2"));
        	}
        	String cookiestr = Constant.LOGIN_COOKIE1+"="+ueditinfo.get(Constant.LOGIN_COOKIE1)+";"+Constant.LOGIN_COOKIE2+"="+ueditinfo.get(Constant.LOGIN_COOKIE2);
			Header[] headers = new BasicHeader[2];//("X-Online-Host",  format_CMWAP_ServerName(strPath));  
			headers[0]=new BasicHeader("Cookie", cookiestr);
			headers[1]=new BasicHeader("Accept-Language","zh-CN");
        	HttpResponse httpResponse = HttpUtils.doHttpExecute(getHttpClient(), Constant.URL_UPDATE_USERINFO, values,headers, HttpUtils.POST,"GBK");
        	if (httpResponse != null && httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        		String responseStr = EntityUtils.toString(httpResponse.getEntity());
				String code = "";
				JSONObject jsonobj; 
				if(responseStr!=null){
					try {
						 jsonobj = new JSONObject(responseStr);
						 result = jsonobj.getString("code");
						 
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}finally {
			            closeHttpClient();
			        }
				}
        	}
        }catch(Exception ex){
        	}
        return result;
        }

}
